/*
 * $Id: StatusReportParser.java,v 1.4 2009/07/21 19:37:40 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.xml.parser;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.onwatch.xml.tag.ResultTag;
import com.ge.healthcare.autosc.onwatch.xml.tag.StatusReport;
import com.ge.healthcare.autosc.onwatch.xml.tag.TaskInfoTag;
import com.ge.healthcare.autosc.onwatch.xml.tag.TaskListTag;
import com.ge.healthcare.autosc.onwatch.xml.tag.TaskResultStatusTag;
import com.ge.healthcare.autosc.onwatch.xml.tag.TaskResultTag;

public class StatusReportParser extends BaseXmlParser {

	public static final String STATUS_REPORT_PARSER = StatusReportParser.class.getName();
	private static final String STATUS_REPORT_ELEMENT = "statusReport";
	private static final String TASKLIST_ELEMENT = "taskList";
	private static final String TASKLIST_STATUS_ELEMENT = "taskListStatus";
	private static final String TASK_ELEMENT = "task";
	private static final String INFO_ELEMENT = "info";
	private static final String OVERALL_TASK_STATUS_ELEMENT = "overAllTaskStatus";
	private static final String STATUS_ELEMENT = "status";
	private static final String RESULT_ELEMENT = "result";
	private StatusReport statusReport = null;
	private TaskListTag taskListTag = null;
	private TaskResultTag taskResultTag = null;
	private TaskInfoTag taskInfoTag = null;
	private TaskResultStatusTag taskResultStatusTag = null;
	private ResultTag resultTag = null;
	
	public StatusReportParser(String schemaFileStr) throws ParserConfigurationException, SAXException {
		super(schemaFileStr);
		statusReport = new StatusReport();
	}
	
	/**
	 * Selecting the parseElement
	 * 
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
	 *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String strNamespaceURI, String strLocalName,
			String strQName, Attributes attrs) throws SAXException {
		super.startElement(strNamespaceURI, strLocalName, strQName, attrs);
		if (STATUS_REPORT_ELEMENT.equals(strQName)) {
			//No attribute in StatusReport element in this version
			strParseElement = STATUS_REPORT_ELEMENT;
		} else if (TASKLIST_ELEMENT.equals(strQName)) {
			strParseElement = TASKLIST_ELEMENT;
			taskListTag = new TaskListTag();
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("systemId"))) {
				taskListTag.setSystemId(attrs.getValue("systemId"));
			}
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("iipVersion"))) {
				taskListTag.setIipVersion(attrs.getValue("iipVersion"));
			}
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("id"))) {
				taskListTag.setId(attrs.getValue("id"));
			}
		} else if (TASKLIST_STATUS_ELEMENT.equals(strQName)) {
			strParseElement = TASKLIST_STATUS_ELEMENT;
			taskListTag.markAsFailure();
		} else if (TASK_ELEMENT.equals(strQName)) {
			strParseElement = TASK_ELEMENT;
			taskResultTag = new TaskResultTag();
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("id"))) {
				taskResultTag.setId(attrs.getValue("id"));
			}
		} else if (INFO_ELEMENT.equals(strQName)) {
			strParseElement= INFO_ELEMENT;
			taskInfoTag = new TaskInfoTag();
		} else if (OVERALL_TASK_STATUS_ELEMENT.equals(strQName)) {
			strParseElement = OVERALL_TASK_STATUS_ELEMENT;
		} else if (STATUS_ELEMENT.equals(strQName)) {
			strParseElement = STATUS_ELEMENT;
			taskResultStatusTag = new TaskResultStatusTag();
		} else if(RESULT_ELEMENT.equals(strQName)) {
			strParseElement = RESULT_ELEMENT;
			resultTag = new ResultTag();
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("id"))) {
				resultTag.setId(attrs.getValue("id"));
			}
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("code"))) {
				resultTag.setCode(attrs.getValue("code"));
			}
			if(!ASCUtil.isNullOrEmpty(attrs.getValue("sec"))) {
				resultTag.setTimeInSec(attrs.getValue("sec"));
			}
		}
	}
	
	/**
	 * Storing the values
	 * 
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public void endElement(String strNamespaceURI, String strLocalName,
			String strQName) throws SAXException {
		super.endElement(strNamespaceURI, strLocalName, strQName);
		if (TASKLIST_ELEMENT.equals(strQName)) {
			statusReport.setTaskList(taskListTag);
		} else if (TASKLIST_STATUS_ELEMENT.equals(strQName)) {
			taskListTag.setTaskListStatus(strElementValue);
			strElementValue = null;
		} else if (TASK_ELEMENT.equals(strQName)) {
			taskListTag.setTaskResult(taskResultTag);
		} else if (OVERALL_TASK_STATUS_ELEMENT.equals(strQName)) {
			taskInfoTag.setOverallTaskStatusDesc(strElementValue);
			taskResultTag.setTaskInfo(taskInfoTag);
			strElementValue = null; 
		} else if (STATUS_ELEMENT.equals(strQName)) {
			taskResultTag.setTaskResultStatus(taskResultStatusTag);
		} else if(RESULT_ELEMENT.equals(strQName)) {
			taskResultStatusTag.add(resultTag);
		}
	}

	/**
	 * Extracting the value
	 * 
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] arrCh, int iStart, int iLength)
			throws SAXException {
		super.characters(arrCh, iStart, iLength);
		if ((OVERALL_TASK_STATUS_ELEMENT.equals(strParseElement)) ||
			(TASKLIST_STATUS_ELEMENT.equals(strParseElement))){
			if (strElementValue != null) {
				strElementValue = new StringBuffer().append(strElementValue)
						.append(new String(arrCh, iStart, iLength)).toString();
			} else {
				strElementValue = new String(arrCh, iStart, iLength);
			}
		}
	}
	
	public void error(SAXParseException spe) throws SAXParseException {
		ASCLogger.fatal(this.getClass(), "error", "Validation failed!!", spe);
		throw spe;	
	}
	
	/**
	 * Invokes the parser to do generic parsing for status report
	 * @param strXML
	 * @return
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public StatusReport processResponse(String strXML) throws SAXException,
			IOException {
		this.parseResponse(strXML);
		return statusReport;
	}
	
	/**
	 * Invokes the parser to do generic parsing for status report
	 * @param strXML
	 * @return
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public StatusReport processResponse(File xmlFile) throws SAXException,
			IOException {
		this.parseResponse(xmlFile);
		return statusReport;
	}
}
